import os
import sys

sys.path.append(os.path.abspath('..\\'))
print(os.path.abspath('.'))
from split.cut_sentence import CutSentence as CS
from data.data_ops import Data_ops


def a_res_to_str(cut_list: list):
    # 输入格式 [['稳住', '！', '别浪', '！'], ['v', 'w', 'v', 'w']]
    res = ""
    for words in cut_list[0]:
        res += words
        res += ' '
    res += '\n'
    for adjs in cut_list[1]:
        res += adjs
        res += ' '
    return res


def show_percent(up: int, down: int):
    return up / down


def main():
    cs = CS()
    # ################################# #
    # 一批的数量
    # 注意，此值应该和 cut_sentence中的值一致
    max_batch = 100
    # ################################# #
    if len(sys.argv) == 1:
        print(
            """
用法:
    go_for_cut.py 工作目录
""")
        return
    my_data = Data_ops(index_data=sys.argv[1])
    if my_data.test() is False:
        print("错误的路径")
        return
    else:
        print("已加载工作目录%s", sys.argv[1])

    # 加载
    dat_list = my_data.get_all_path()
    # 切分
    cnt = 0
    while cnt < len(dat_list):
        # 获取文件名
        # print(1)
        batch_res_file_name = dat_list[cnt:cnt + max_batch]
        # print(cnt, cnt+100)
        # print(2)
        # 读取文件内容
        batch_text_list = []
        for file_path in batch_res_file_name:
            batch_text_list.append(my_data.get_file_text(file_path=file_path).split('\t')[-1])
            if batch_text_list[-1] == '':  # 取消空字符串
                batch_text_list[-1] = ' '
        # 切分
        # print(3)
        # print(batch_text_list)
        cut_res = cs.cut_100_sent(batch_text_list)
        # print(4)
        # 保存

        for i in range(len(batch_res_file_name)):
            batch_res_file_name[i] = batch_res_file_name[i].replace('.txt', '.cut')
            with open(batch_res_file_name[i], 'w+', encoding='utf-8')as f_w:
                f_w.write(a_res_to_str(cut_res[i]))
        # print("完成了一次写入.")
        print("%d%% %d %d" % ((cnt / len(dat_list)) * 100, cnt, len(dat_list)))
        cnt += max_batch

    # 保存


if __name__ == '__main__':
    main()
